package com.djc8.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.djc8.entity.Page;
import com.djc8.model.Model;

/**
 * Servlet implementation class SearchBookServlet
 */
public class SearchBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String key=request.getParameter("keyword");
		String cat=request.getParameter("cat");
		StringBuilder sb=new StringBuilder();
		
		if(key!=null && !key.equals(""))
		{
			sb.append(" AND main.Book_Name LIKE '%"+key+"%'");
		}
		if(cat!=null && !cat.equals("")){
			sb.append(" AND main.BookClassify_ID =" +cat );
		}
		
		request.setAttribute("keyword", key);
		request.setAttribute("cat", cat);
		
		Model model=new Model();
		
		Page page=model.doPageForBook(1, 100, sb.toString());
		
		if(page.getPageList()!=null && page.getPageList().size()>0)
			request.setAttribute("searchBookList", page.getPageList());
		else
			request.setAttribute("searchBookList", null);
		request.getRequestDispatcher("WEB-INF/jsp/SearchBook.jsp").forward(request, response);
		
	}

}
